package io.renren.modules.xiaoqu.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mchange.lang.IntegerUtils;
import io.renren.common.validator.Assert;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.xiaoqu.entity.CostEntity;
import io.renren.modules.xiaoqu.service.CostService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 费用信息表
 *
 * @author yan
 * @email 2425784791@qq.com
 * @date 2022-05-19 21:56:53
 */
@Api
@RestController
@RequestMapping("xiaoqu/cost")
public class CostController {
    @Autowired
    private CostService costService;

    @Autowired
    private UserService userService;
//    /**
//     * 列表
//     */
//    @RequestMapping("/list")
//    @RequiresPermissions("xiaoqu:cost:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = costService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }
//
//
//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{id}")
//    @RequiresPermissions("xiaoqu:cost:info")
//    public R info(@PathVariable("id") Integer id){
//		CostEntity cost = costService.getById(id);
//
//        return R.ok().put("cost", cost);
//    }
//
//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    @RequiresPermissions("xiaoqu:cost:save")
//    public R save(@RequestBody CostEntity cost){
//		costService.save(cost);
//
//        return R.ok();
//    }
//
//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    @RequiresPermissions("xiaoqu:cost:update")
//    public R update(@RequestBody CostEntity cost){
//		costService.updateById(cost);
//
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
//    @RequiresPermissions("xiaoqu:cost:delete")
//    public R delete(@RequestBody Integer[] ids){
//		costService.removeByIds(Arrays.asList(ids));
//
//        return R.ok();
//    }

    /**
     * 物业费用查询
     */
    @GetMapping("getList")
    public R getList(String name,Integer status,int page,int pageSize){

        //根据用户名查询用户
        LambdaQueryWrapper<UserEntity> queryWrapper=new LambdaQueryWrapper<>();
        //如果name不是空的就做模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name),UserEntity::getUsername,name);
        List<UserEntity> userEntityList = userService.list(queryWrapper);

        //存放根据用户名模糊查询到 结构id
        List<Long> ids=new ArrayList<>();

        for (UserEntity item:userEntityList
             ) {
            ids.add(item.getUserId());
        }

        //缴费状态
        LambdaQueryWrapper<CostEntity> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(status!=null,CostEntity::getStatus,status);
        queryWrapper1.in(CostEntity::getUserId,ids);

        //分页查询
        Page<CostEntity> pageUtil=new Page<>(page,pageSize);
        Page<CostEntity> costEntityPage = costService.page(pageUtil, queryWrapper1);

        //现在    page结果集中  record中没有业主名这个信息

        for (CostEntity item:costEntityPage.getRecords()
             ) {
            //根据userid查询用户名
            UserEntity byId = userService.getById(item.getUserId());
            item.setUsername(byId.getUsername());
            item.setNumber(byId.getNumber());
        }

        return R.ok().put("data",costEntityPage);
    }

    /**
     * 删除
     */
    @ApiOperation("delete")
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		costService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @ApiOperation("update")
    @PostMapping("/update")
    public R update (@RequestBody CostEntity costEntity){
        costService.updateById(costEntity);
        return R.ok();
    }

    @ApiOperation("save")
    @PostMapping("/save")
    public R save (@RequestBody CostEntity costEntity){
        LambdaQueryWrapper<UserEntity> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(StringUtils.isNotBlank(costEntity.getUsername()),UserEntity::getUsername,costEntity.getUsername());
        if (queryWrapper==null){
            return R.error("没有此用户,请先注册改用户名");
        }
        Long userId = userService.getOne(queryWrapper).getUserId();
        costEntity.setUserId(userId);
        costService.save(costEntity);
        return R.ok();
    }

}
